package fyp.spedmew;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MarkerOptions;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import java.util.List;

/**
 * Created by ChengYS on 17/1/2015.
 */
public class MapActivity extends Activity {

    /** Local variables **/
    GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        createMapView();
        addMarker();
    }

    /**
     * Initialises the mapview
     */
    private void createMapView(){
        /**
         * Catch the null pointer exception that
         * may be thrown when initialising the MapActivity
         */
        try {
            if(null == googleMap){
                googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                        R.id.mapView)).getMap();

                /**
                 * If the MapActivity is still null after attempted initialisation,
                 * show an error to the user
                 */
                if(null == googleMap) {
                    Toast.makeText(getApplicationContext(),
                            "Error creating MapActivity",Toast.LENGTH_SHORT).show();
                }
            }
        } catch (NullPointerException exception){
            Log.e("mapApp", exception.toString());
        }
    }

    /**
     * Adds a marker to the MapActivity
     */
    private void addMarker(){


        LatLngBounds.Builder builder = new LatLngBounds.Builder();


        ParseQuery<ParseObject> query = ParseQuery.getQuery("Location");
        query.whereEqualTo("JobID", "3");
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> LocationList, ParseException e) {

                if (e == null) {
                    Number _Latitude = LocationList.get(0).getNumber("Latitude");

                    Number _Longitude = LocationList.get(0).getNumber("Longitude");

                    LatLng location = new LatLng((Double) _Latitude, (Double) _Longitude);

                    /** Make sure that the MapActivity has been initialised **/
                    if (null != googleMap) {
                        googleMap.addMarker(new MarkerOptions()
                                        .position(location)
                                        .title("Marker")
                                        .draggable(true)
                        );
                    }


                    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 15));


                    Log.d("score", "Retrieved " + LocationList.size() + " scores");
                } else {
                    Log.d("score", "Error: " + e.getMessage());
                }
            }
        });




     //   LatLng location = new LatLng(1.4431087,103.78540406);

        /** Make sure that the MapActivity has been initialised **/
 //       if(null != googleMap){
  //          googleMap.addMarker(new MarkerOptions()
   //                         .position(location)
   //                         .title("Marker")
   //                         .draggable(true)
     //       );
    //    }


  //      googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 15));


    }


//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//
//        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.main, menu);
//        return true;
//    }
//
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//        // Handle action bar item clicks here. The action bar will
//        // automatically handle clicks on the Home/Up button, so long
//        // as you specify a parent activity in AndroidManifest.xml.
//        int id = item.getItemId();
//        if (id == R.id.action_settings) {
//            return true;
//        }
//        return super.onOptionsItemSelected(item);
//    }



}